package Practica03;

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Eje01 {
	
	public static void main(String[] args) {
		MiVentana v = new MiVentana("Claculo IMC [Indice de Masa Corporal]");
		v.Iniciar();
	}

}

class IMC{
	
	public static double CalcularIMC(double peso, double altura){
		return peso/Math.pow(altura, 2);
	}
	
	public static String Diagnostico(double imc){
		String tmp="";
		
		if(imc < 16)
			tmp = "Criterio de ingreso al hospistal.";
		if(imc>=16 && imc<17)
			tmp = "Infrapeso.";
		if(imc>=17 && imc<18)
			tmp = "Bajo peso.";
		if(imc>=18 && imc<25)
			tmp = "Peso normal (Saludable).";
		if(imc>=25 && imc<30)
			tmp = "Sobrepeso (obesidad de grado I).";
		if(imc>=30 && imc<35)
			tmp = "Sobrepeso cronico (obesidad de grado II).";
		if(imc>=35 && imc<40)
			tmp = "Obesidad premorbida (obesidad de grado III).";
		if(imc>=40)
			tmp = "Obesidad morbida (obesidad de grado IV).";
		
		return tmp;
	}
}

class MiVentana extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6257531545674631824L;
	private JLabel lblPeso;
	private JLabel lblAltura;	
	private JTextField txtPeso;
	private JTextField txtAltura;	
	private JButton btnCalcular;
	private JButton btnCerrar;
	
	public MiVentana(String titulo){
		super(titulo);
		
		lblAltura = new JLabel("Altura [Metros]:");
		lblPeso = new JLabel("Peso [Kg]:");		
		txtAltura = new JTextField(20);
		txtPeso= new JTextField(20);		
		btnCalcular = new JButton("Calcular");
		btnCerrar = new JButton("Cerrar");
		
		btnCalcular.addActionListener(this);
		btnCerrar.addActionListener(this);
		
		FlowLayout fl = new FlowLayout(FlowLayout.RIGHT);
		
		JPanel pa = new JPanel(fl);
		pa.add(lblAltura);
		pa.add(txtAltura);
		
		JPanel pp = new JPanel(fl);
		pp.add(lblPeso);
		pp.add(txtPeso);		
		
		JPanel pb = new JPanel(fl);
		pb.add(btnCalcular);
		pb.add(btnCerrar);
		
		Container contenedor = getContentPane();
		contenedor.setLayout(new BoxLayout(contenedor, BoxLayout.Y_AXIS));
		contenedor.add(pa);
		contenedor.add(pp);		
		contenedor.add(pb);
	}
	
	public void actionPerformed(ActionEvent e) {
		
		if(e.getSource()== btnCalcular){
			
			double tmpa = 0.0;
			double tmpp = 0.0;
			boolean error = false;
			
			try{
				tmpa = Double.parseDouble(txtAltura.getText());
				error = false;
			}catch(NumberFormatException ex){
				error = true;
			}
			
			try{
				tmpp = Double.parseDouble(txtPeso.getText());
				error = false;
			}catch(NumberFormatException ex){
				error = true;
			}
			
			if(error){
				JOptionPane.showMessageDialog(this, "Datos no validos", "Error", JOptionPane.WARNING_MESSAGE);
			}else{
				StringBuilder sb = new StringBuilder();
				double tmp = ((int)(IMC.CalcularIMC(tmpp, tmpa)*100))/100;
				sb.append("IMC = " + Double.toString(tmp)+ "\n");
				sb.append(IMC.Diagnostico(tmp));
				JOptionPane.showMessageDialog(this, sb.toString(), "Diagnostico", JOptionPane.INFORMATION_MESSAGE);
			}
		}
		
		if(e.getSource() == btnCerrar){
			this.dispose();
		}
	}
	
	public void Iniciar(){
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
		setResizable(false);		
		setLocationByPlatform(true);
		setVisible(true);
	}
	
}